/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.discount.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.medusa.gruul.common.core.constant.CommonConstants;
import com.medusa.gruul.common.core.util.LocalDateTimeUtils;
import com.medusa.gruul.discount.api.model.vo.manager.MemberCouponVo;
import com.medusa.gruul.discount.service.manager.IMemberCouponService;
import com.medusa.gruul.discount.web.enums.CouponEffectiveTypeEnum;
import com.medusa.gruul.discount.web.enums.CouponStatusEnum;
import com.medusa.gruul.discount.web.enums.MemberCouponUseStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.TimeZone;

/**
 * @description: UpdateCouponTask.java
 * @author: lcysike
 * @date: 2019/12/22 21:41
 */
@Slf4j
@Component
public class UpdateMemberCouponTask {

    @Resource
    private IMemberCouponService memberCouponService;

    @Scheduled(fixedRate = 1000 * 60 * 5)
    public void updateMemberCouponTask() {
        log.info("-----------客户优惠劵定时任务开启-----------");
        List<MemberCouponVo> memberCouponVos = memberCouponService.findMemberCouponListForTask();
        if(CollectionUtil.isNotEmpty(memberCouponVos)){
            memberCouponVos.stream().forEach(memberCouponVo -> {
                Long nowTime = System.currentTimeMillis();
                //判断有效时间类型
                if(CouponEffectiveTypeEnum.TIME_BEGIN.getStatus() == memberCouponVo.getEffectiveType()){
                    //判断时间是否在有效期内  在的话优惠劵状态为活动中 开始时间取当天的0点时间 结束时间取结束时间当天的23：59：59秒
                    Long startTime = DateUtil.beginOfDay(memberCouponVo.getStartTime()).getTime();
                    Long endTime = DateUtil.endOfDay(memberCouponVo.getEndTime()).getTime();
                    if(nowTime < startTime){
                        memberCouponVo.setStatus(CouponStatusEnum.BE_GOING.getStatus());
                    }else if(nowTime > startTime && nowTime < endTime){
                        memberCouponVo.setStatus(CouponStatusEnum.ON_GOING.getStatus());
                    }else if (nowTime > endTime){
                        memberCouponVo.setStatus(CouponStatusEnum.OVER.getStatus());
                        memberCouponVo.setUseStatus(MemberCouponUseStatusEnum.HAS_EXPIRE.getStatus());
                    }
                }else if(CouponEffectiveTypeEnum.NOW_BEGIN.getStatus() == memberCouponVo.getEffectiveType()){
                    //领劵立即生效 判断当前时间是否大于活动结束时间 结束时间是按天计算不是按24小时 取createTime记得取当天的0点时间
                    if(memberCouponVo == null){
                        memberCouponVo.setStatus(CouponStatusEnum.ON_GOING.getStatus());
                    }else{
                        Long dayTime = Long.valueOf(CommonConstants.SECOND_OF_HOUR * CommonConstants.HOUR_OF_DAY);
                        LocalDateTime createTime = LocalDateTimeUtils.getDayStart(memberCouponVo.getCreateTime());
                        Long zero = createTime.toInstant(ZoneOffset.of("+8")).toEpochMilli() / dayTime * dayTime - TimeZone.getDefault().getRawOffset();
                        Long twelve=zero + dayTime - CommonConstants.NUMBER_ONE;
                        Long endTime = twelve + memberCouponVo.getEffectiveDays() * dayTime;
                        if(nowTime < endTime){
                            memberCouponVo.setStatus(CouponStatusEnum.ON_GOING.getStatus());
                        }else{
                            memberCouponVo.setStatus(CouponStatusEnum.OVER.getStatus());
                            memberCouponVo.setUseStatus(MemberCouponUseStatusEnum.HAS_EXPIRE.getStatus());
                        }
                    }
                }else{
                    memberCouponVo.setStatus(CouponStatusEnum.OVER.getStatus());
                    memberCouponVo.setUseStatus(MemberCouponUseStatusEnum.HAS_EXPIRE.getStatus());
                }
                //客户优惠劵更新
                try{
                    memberCouponService.updateMemberCouponForTask(memberCouponVo);
                }catch(Exception e){
                    e.printStackTrace();
                    log.error("客户优惠劵id为"+memberCouponVo.getId()+e.getMessage(), e);
                }
            });
        }
        log.info("-----------客户优惠劵定时任务结束-----------");
    }
}
